package org.ly817.my.spring.core.aop;

import org.aopalliance.intercept.MethodInvocation;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;

/**
 * 反射实现的方法调用
 * MethodInvocation 方法调用
 * aop提供的协议
 * MethodInvocation-Invocation-Joinpoint
 * 方法调用-调用-接入点
 *
 * @author LuoYu
 * @date 2018/4/15
 */
public class ReflectiveMethodInvocation implements MethodInvocation {

    /**
     * 切入点的对象
     */
    private Object target;

    /**
     * 切入点的方法
     */
    private Method method;

    /**
     * 切入点方法的参数
     */
    private Object[] args;

    public ReflectiveMethodInvocation(Object target, Method method, Object[] args) {
        this.target = target;
        this.method = method;
        this.args = args;
    }

    /**
     * 来自MethodInvocation接口
     *
     * @return
     */
    @Override
    public Method getMethod() {
        return method;
    }

    /**
     * 来自Invocation接口
     *
     * @return
     */
    @Override
    public Object[] getArguments() {
        return args;
    }

    /**
     * 来自Joinpoint接口
     *
     * @return
     * @throws Throwable
     */
    @Override
    public Object proceed() throws Throwable {
        return method.invoke(target, args);
    }

    /**
     * 来自Joinpoint接口
     *
     * @return
     */
    @Override
    public Object getThis() {
        return target;
    }

    /**
     * 来自Joinpoint接口
     *
     * @return
     */
    @Override
    public AccessibleObject getStaticPart() {
        return method;
    }
}
